<?php
namespace app\v1\controller;

use think\Controller;

class Weixin extends Controller
{
	public function log($data='')
	{
		file_put_contents('./log.txt',$data);
	}
    
    public function index(){

    	if(input('echostr')){

    		if($this->checkSign()){
    			exit(input('echostr'));
    		}else{
    			exit('error');
    		}
    	}else{
            // $this->respon();
    		if($this->checkSign()){
    			$this->respon();
    		}else{
    			exit(json_encode(['code'=>0,'msg'=>'非法请求']));
    		}
    	}
    	// echo input('echostr');exit;
    	// $this->log(input(''));
    }

    public function respon()
    {
    	$postStr = $GLOBALS["HTTP_RAW_POST_DATA"];

        // $this->log($postStr);exit;

    	if (!empty($postStr)){
                
            $postObj = simplexml_load_string($postStr,'SimpleXMLElement', LIBXML_NOCDATA);

            switch ($postObj->MsgType) {
            	case 'text':
            		$this->responText($postObj);
            		break;
            	case 'image':
            		$this->responImage($postObj);
            		break;
            	case 'voice':
            		# code...
            		break;
            	case 'video':
            		# code...
            		break;
            	default:
            		$this->responText();
            		break;
            }

            
        }

    	
    }

    public function responText($postObj)
    {

        if($postObj->Content=="图片"){
            $postObj->MediaId = "qc68QGR17bHHZckEdSPJa-QU2hFvShCWrFmJvHN4y2rmpcIdJhtKEJ9aCRJfB5IL";
            $this->responImage($postObj);
        }

        if($postObj->Content=="恭喜"){
            $this->responVoice($postObj);
        }

    	$text = "你也好";

    	$str = "<xml><ToUserName><![CDATA[".$postObj->FromUserName."]]></ToUserName><FromUserName><![CDATA[".$postObj->ToUserName."]]></FromUserName><CreateTime>".time()."</CreateTime><MsgType><![CDATA[text]]></MsgType><Content><![CDATA[".$text."]]></Content></xml>";

        exit($str);

    	// $this->log($str);

    }
     public function responImage($postObj)
    {

        $str = "<xml><ToUserName><![CDATA[".$postObj->FromUserName."]]></ToUserName><FromUserName><![CDATA[".$postObj->ToUserName."]]></FromUserName><CreateTime>".time()."</CreateTime><MsgType><![CDATA[image]]></MsgType><Image><MediaId><![CDATA[".$postObj->MediaId."]]></MediaId></Image></xml>";

        exit($str);

        $this->log($str);

    }

    public function responVoice($postObj)
    {
        $id = "sH4NQhHg4xTF8HtPIAMHtNwnbgGJd-2eqjmAxt9R6j0N3h1P6VwSJ9AzQ50ICBWl";

        $str = "<xml><ToUserName><![CDATA[".$postObj->FromUserName."]]></ToUserName><FromUserName><![CDATA[".$postObj->ToUserName."]]></FromUserName><CreateTime>".time()."</CreateTime><MsgType><![CDATA[voice]]></MsgType><Voice><MediaId><![CDATA[".$id."]]></MediaId></Voice></xml>";

        $this->log($str);
        exit($str);


    }

    public function checkSign()
    {
    	$signature = input('signature');
        $timestamp = input('timestamp');
        $nonce = input('nonce');
        $token = TOKEN;

        $tmpArr = array($token, $timestamp, $nonce);

        sort($tmpArr);

        $tmpStr = implode($tmpArr);

        $tmpStr = sha1($tmpStr);
        
        if( $tmpStr == $signature ){
            return true;
        }else{
            return false;
        }
    }
}